package com.shuziren.controller.QuestionController;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuziren.abnormal.CustomException;
import com.shuziren.core.vo.Result;
import com.shuziren.question.entity.dto.InsertInterviewQuestionDTO;
import com.shuziren.question.entity.dto.InterviewQuestionDTO;
import com.shuziren.question.entity.InterviewQuestion;
import com.shuziren.question.entity.dto.SelectInterviewQuestionDTO;
import com.shuziren.question.entity.dto.UpdateInterviewQuestionDTO;
import com.shuziren.question.entity.vo.SelectByQuestion;
import com.shuziren.question.service.InterviewQuestionService;


import jakarta.validation.constraints.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理题目相关的HTTP请求
 * @author 123
 */
@RestController
@RequestMapping("/api/question")
public class InterviewQuestionController {
    
    @Autowired
    private InterviewQuestionService questionService;


    /**
     * 分页查询题目列表
     * @param interviewQuestion 包含查询条件的请求体
     * @return 返回分页查询结果
     */
    @PostMapping("/getAllQuestions")
    public Result<IPage<InterviewQuestion>> getAllQuestions(@RequestBody @Validated SelectInterviewQuestionDTO interviewQuestion) {
        try {
            IPage<InterviewQuestion> result = questionService.getAllQuestions(interviewQuestion);
            if (result.getRecords().isEmpty()) {
                return Result.success(result);
            }
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 添加题目及其关联数据
     * @param questionDTO 包含题目及其关联数据的请求体
     * @return 返回添加结果
     */
    @PostMapping("/add")
    public Result<String> addQuestion(@RequestBody @Validated InsertInterviewQuestionDTO questionDTO) {
        try {
            String s = questionService.addQuestion(questionDTO);
            return Result.success(s);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 根据ID查看详情
     * @param id
     * @return 返回详情结果
     */
    @GetMapping("/questionAndCategory/{id}")
    public Result<SelectByQuestion> getQuestionAndCategoryById(@PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        try {
            SelectByQuestion selectByQuestion = questionService.selectQuestionAndCategoryById(id);
            if (selectByQuestion != null){
                return Result.success(selectByQuestion);
            }else {
                return Result.fail("未查询到该题目");
            }
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

//    {
//        "id": 33,
//            "title": "JAVA",
//            "answer": "123",
//            "description": "123",
//            "tags": "1,2,3,4",
//            "difficulty": 1,
//            "expectedTime": 1,
//            "status": 1,
//            "score": 10,
//            "categories": [
//        19,20
//  ],
//        "questionSets": [
//        30
//  ]
//    }
    /**
     * 修改题目
     * @param updateInterviewQuestionDTO
     * @return 返回修改结果
     */
    @PostMapping("/update")
    public Result<String> updateInterviewQuestion(@RequestBody @Validated UpdateInterviewQuestionDTO updateInterviewQuestionDTO) {

        boolean b = questionService.updateQuestion(updateInterviewQuestionDTO);
        if (b) {
            return Result.success("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }


    /**
     * 根据ID进行删除
     * @param id
     * @return 返回删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteInterviewQuestion(@PathVariable("id") Integer id) {
        try {
            questionService.deleteQuestions(id);
        } catch (CustomException e) {
            throw new RuntimeException(e);
        }
        return Result.success("删除成功");
    }

} 